package com.wolfpire.workflow.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.workflow.dao.WorkflowDao;
import com.wolfpire.workflow.em.WorkflowStatus;
import com.wolfpire.workflow.om.Workflow;

@Repository("workflowDao")
public class WorkflowDaoImpl extends  BaseHibernateDao<Workflow, Long>  implements WorkflowDao {
	
	@SuppressWarnings("unchecked")
	public List<Workflow> queryAppList() {
		Criteria criteria = createCriteria(Restrictions.eq("status", WorkflowStatus.NORMAL.getIndex()));
		return criteria.list();
	}
	
	/**
	 * 根据流程定义ID获取工作流
	 * @param dbId
	 * @return
	 */
	public Workflow getWorkflowByDeploymentId(Long deploymentId){
		Criteria criteria = createCriteria(Restrictions.eq("deploymentId", deploymentId));
		return (Workflow) criteria.uniqueResult();
	}
	
	/**
	 * 根据表单类型ID获取工作流
	 * @param formTypeId
	 * @return
	 */
	public Workflow getWorkflowByFormTypeId(String formTypeId){
		Criteria criteria = createCriteria(Restrictions.eq("formTypeId",formTypeId));
		return (Workflow) criteria.uniqueResult();
	}
	
	/**
	 * 根据表单类型ID和组织ID串查询该组织是否以有一条同一表单类型的流程定义
	 * @param formTypeId
	 * @param respectOrgsId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Workflow> getWorkflowByFormTypeIdAndOrgId(String formTypeId,String orgId){		
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("formTypeId", formTypeId));
		criteria.add(Restrictions.like("respectOrgsId","%"+orgId+"%"));				
		return (List<Workflow>) criteria.list();
	}
	
	/**
	 * 根据流程名称、流程类型Id和组织Id串查询流程定义
	 * @param flowName 流程定义名称
	 * @param wftypeId
	 * @param orgId
	 * @return
	 */
	public Workflow getWorkflowByWfTypeIdAndOrgId(String flowName, String wftypeId, String orgId) {		
		Criteria criteria = createCriteria();
		if(flowName!=null&&!flowName.trim().equals("")){
			criteria.add(Restrictions.eq("flowName", flowName));
		}
		if(wftypeId!=null&&!wftypeId.trim().equals("")){
			criteria.add(Restrictions.eq("wftypeId", wftypeId));
		}
		criteria.add(Restrictions.like("respectOrgsId","%"+orgId+"%"));				
		return (Workflow) criteria.uniqueResult();
	}

	@Override
	public Page<Workflow> findWorkflows(EasyuiPage<Workflow> page, Workflow filterWorkflow) {
		Criterion[] criterions = createFilter(filterWorkflow);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		
		if (0 < totalCount) {
			Criteria c = createCriteria(criterions);
			c = this.setPageParameter(c, page);
			@SuppressWarnings({ "unchecked" })
			List<Workflow> result = c.list();
			page.setDataList(result);
		}
		return page;
	}

	private Criterion[] createFilter(Workflow filterWorkflow) {
		if (null == filterWorkflow) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterWorkflow.getWorkFlowId()) {
			criterions.add(Restrictions.eq("workFlowId", filterWorkflow.getWorkFlowId()));
		}
		if (!StringUtils.isBlank(filterWorkflow.getFlowName())) {
			criterions.add(Restrictions.eq("flowName", filterWorkflow.getFlowName()));
		}
		if (null != filterWorkflow.getDeploymentId()) {
			criterions.add(Restrictions.eq("deploymentId", filterWorkflow.getDeploymentId()));
		}
		if (!StringUtils.isBlank(filterWorkflow.getStatus())) {
			criterions.add(Restrictions.eq("status", filterWorkflow.getStatus()));
		}
		if (!StringUtils.isBlank(filterWorkflow.getWftypeId())) {
			criterions.add(Restrictions.eq("wftypeId", filterWorkflow.getWftypeId()));
		}
		if (!StringUtils.isBlank(filterWorkflow.getFormTypeId())) {
			criterions.add(Restrictions.eqOrIsNull("formTypeId", filterWorkflow.getFormTypeId()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Workflow> list(Workflow workflow) {
		Criteria c = createCriteria(this.createFilter(workflow));
		return c.list();
	}

}
